package com.zksk.portal.controller.invoice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.api.RemoteInvoiceRecordService;
import com.zksk.common.core.constant.SecurityConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.common.core.web.controller.BaseController;

import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.Member;
import com.zksk.mbg.admin.domain.MemberInvoice;
import com.zksk.mbg.admin.domain.MemberInvoiceRecord;
import com.zksk.mbg.admin.domain.Orders;
import com.zksk.portal.domain.invoice.dto.InvoiceApplyDto;
import com.zksk.portal.domain.invoice.vo.InvoiceApplyVo;
import com.zksk.portal.domain.invoice.vo.InvoiceOrdersVo;
import com.zksk.portal.service.invoice.InvoiceApplyService;
import com.zksk.portal.service.invoice.MemberInvoiceService;
import com.zksk.portal.service.member.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/invoice")
@Api(tags = "发票申请相关接口")
public class InvoiceController extends BaseController {
    @Autowired
    private InvoiceApplyService invoiceApplyService;

    @Autowired
    private MemberInvoiceService memberInvoiceService;

    @Autowired(required = false)
    private RemoteInvoiceRecordService remoteInvoiceRecordService;

    @Autowired
    private MemberService memberService;

    @Log(title = "发票申请列表展示",businessType = BusinessType.SELECT)
    @PostMapping("/list")
    @ApiOperation(value = "查询发票申请列表")
    public R<IPage<InvoiceApplyVo>> list(@RequestBody InvoiceApplyDto invoiceApplyDto){
        if (invoiceApplyDto.getMemberId()==null){
            return R.fail("获取发票申请列表失败");
        }
        IPage<InvoiceApplyVo> applyVoPage = invoiceApplyService.invoiceList(invoiceApplyDto);
        return R.ok(applyVoPage,"获取发票申请列表数据成功");
    }

    @Log(title = "查询发票详细信息",businessType = BusinessType.SELECT)
    @GetMapping("/{ordersId}")
    @ApiOperation(value = "根据订单id查询发票详细信息")
    public R<InvoiceOrdersVo> getById(@PathVariable Long ordersId){
        InvoiceOrdersVo invoiceVo = new InvoiceOrdersVo();
        //获取订单信息
        Orders orders = invoiceApplyService.getById(ordersId);
        //获取用户发票信息
        Long memberId = orders.getMemberId();
        QueryWrapper<MemberInvoice> queryWrapper = new QueryWrapper<MemberInvoice>()
                .eq(memberId!=null,"member_id",memberId);
        MemberInvoice memberInvoice = memberInvoiceService.getOne(queryWrapper);
        //获取用户开票记录信息
        R<MemberInvoiceRecord> recordR = remoteInvoiceRecordService.getByOrdersId(ordersId, SecurityConstants.INNER);
        //获取企业信息
        Member member = memberService.getById(memberId);
        invoiceVo.setMemberInvoice(memberInvoice);
        invoiceVo.setMemberInvoiceRecord(recordR.getData());
        invoiceVo.setOrders(orders);
        invoiceVo.setMember(member);
        return R.ok(invoiceVo,"获取发票详细信息成功");
    }

    @Log(title = "提交发票申请",businessType = BusinessType.INSERT)
    @PostMapping("/apply")
    @ApiOperation(value = "提交发票申请")
    public R<MemberInvoiceRecord> apply(@RequestBody MemberInvoiceRecord memberInvoiceRecord){

        Long memberId = memberInvoiceRecord.getMemberId();
        Member member = memberService.getById(memberId);
        memberInvoiceRecord.setAddress(member.getAddress());//企业地址
        memberInvoiceRecord.setTRNumber(member.getCreditCode());//纳税人识别号
        Orders orders = invoiceApplyService.getById(memberInvoiceRecord.getOrdersId());
        memberInvoiceRecord.setAmount(orders.getPremiumAmount());//发票金额
        memberInvoiceRecord.setStatus(2);//已申请
        remoteInvoiceRecordService.saveInvoiceRecord(memberInvoiceRecord, SecurityConstants.INNER);
        memberInvoiceService.saveMemberInvoice(memberInvoiceRecord);
        return R.ok(memberInvoiceRecord,"提交发票申请成功");

    }
    @Log(title = "根据企业id查看开票信息是否完整",businessType = BusinessType.SELECT)
    @GetMapping("/exist/{memberId}")
    @ApiOperation(value = "根据企业id查看所需开票信息是否完整")
    public R<Member> selectInvoice(@PathVariable Long memberId){
        Member member = memberService.getById(memberId);

        String openBank = member.getOpenBank();//开户行名称
        String bankAccount = member.getBankAccount();//开户行账号
        String phone = member.getPhone();//企业电话
        if (StringUtils.isEmpty(openBank)||StringUtils.isEmpty(bankAccount)||StringUtils.isEmpty(phone)){
            return R.fail("开票信息不完整");
        }
        return R.ok(member,"开票信息完整");
    }




}
